package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ExtraValueContent;

/**
 * 对象功能:  奖惩内容查询界面
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/6/20 18:09
 */
public class ScoreContentQueryFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      ScoreContentQueryFragment.class.getSimpleName() + "EXTRA_CONTENT";

  private final int REQUEST_KIND = 1;
  @BindView(R.id.tv_rewardKind) TextView tvRewardKind;
  @BindView(R.id.ib_rewardKind) ImageView ibRewardKind;
  @BindView(R.id.et_rewardContent) EditText etRewardContent;

  private String rewardKindCode;
  private ExtraValueContent queryConten;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
  }

  @Override public void initData() {
    queryConten = (ExtraValueContent) getArguments().getSerializable(EXTRA_CONTENT);
    if (queryConten != null) {
      rewardKindCode = queryConten.getExtCode1();
    } else {
      rewardKindCode = "";
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        ExtraValueContent queryConten = new ExtraValueContent();
        queryConten.setExtName1(tvRewardKind.getText().toString());
        queryConten.setExtCode1(rewardKindCode);
        queryConten.setExtName2(etRewardContent.getText().toString());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_CONTENT, queryConten);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = super.onCreateView(inflater, container, savedInstanceState);
    ButterKnife.bind(this, rootView);
    return rootView;
  }

  @Override protected int initLayoutId() {
    return R.layout.fragment_score_content_query;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    tvRewardKind.setText(queryConten.getExtName1());
    etRewardContent.setText(queryConten.getExtName2());
  }

  @OnClick(R.id.ib_rewardKind) public void onClick() {
    //奖惩内容
    showDialog(new RewardKindDialogFragment(), REQUEST_KIND);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_KIND) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      rewardKindCode = customer.getId();
      tvRewardKind.setText(customer.getName());
    }
  }

  public static ScoreContentQueryFragment newInstance(ExtraValueContent queryConten) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, queryConten);
    ScoreContentQueryFragment fragment = new ScoreContentQueryFragment();
    fragment.setArguments(args);
    return fragment;
  }
}


